/*
 * @Author: ssssnack
 * @Date: 2019-05-08 09:31:06
 * @Description: 基础配置信息
 */
const api = require('/utils/api.js')
const config = require('/shopInfo.js')
App({
  onLaunch: function (options) {
    console.log(options)
    console.log(options.query.parentId)
    let scene
    if(options.query.scene){
      scene = decodeURIComponent(options.query.scene)
    }
    
    console.log(scene)
    let this_parentId = options.query.parentId || scene
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var that = this;
        that.globalData.code = res.code;
        wx.request({
          url: api.baseUrl + 'user/getWxOpenId',
          data: {
            code: res.code,
            shopId: that.globalData.shopId,
            parentId: this_parentId || ''
          },
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            var obj = {};
            obj.openid = res.data.data.openid;
            obj.userId = res.data.data.userId;
            obj.session_key = res.data.data.sessionKey;
            obj.mobileGrant = res.data.data.mobileGrant;
            that.globalData.phone = res.data.data.mobile;
            that.globalData.userId = res.data.data.userId;
            that.globalData.openid = res.data.data.openid;
            that.globalData.mobileGrant = res.data.data.mobileGrant;
            wx.setStorageSync('user', obj); //存储openid     
            wx.setStorageSync('phone', res.data.data.mobile);
            if(that.userInfoCallback){
              that.userInfoCallback(res)
            }

          }
        })
      }
    })

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.globalData.userInfo = res.userInfo
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })



  },
  onShow: function () {

  },
  globalData: {
    userInfo: null,
    openId: '',
    code: '',
    userId: '',
    mobileGrant: '',
    phone: '',
    /*  【配置商户信息】 */
    appid: config.shopInfo.appid, // 小程序 appid
    appsecret: config.shopInfo.appsecret, // 小程序 appsecret
    shopId: config.shopInfo.shopId, // 商户 id
    cardId: config.shopInfo.cardId, // 会员卡id，公众号内可查看
    appName: config.shopInfo.appName // 小程序名称
  }
})